home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: Norman Bullen <nbullen@ix.netcom.com>
- Newsgroups: comp.lang.c
- Subject: Re: very simple newbie question
- Date: Thu, 29 Feb 1996 06:34:47 -0800
- Organization: Black Cat Associates
- Message-ID: <3135B987.1DDE@ix.netcom.com>
- References: <robertk-2802960012350001@robertk.accessone.com> <TANMOY.96Feb28090444@qcd.lanl.gov>
- NNTP-Posting-Host: ple-ca9-19.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-NETCOM-Date: Thu Feb 29 6:43:39 AM PST 1996
- X-Mailer: Mozilla 2.0 (Win16; I)
- CC: robertk@accessone.com
-
- Tanmoy Bhattacharya wrote:
- >
- > In article <robertk-2802960012350001@robertk.accessone.com>
- > robertk@accessone.com (robertk) writes:
- > <snip>
- > r: #include <stdio.h>
- > r: main()
- > r: {
- > r: char s[10];
- > r: scanf("%s/n",s);
- > r: printf("%s",s);
- > r: }
- > r:
- > r: when i run this and input:
- > r:
- > r: robert <enter> --->when i hit the enter key the cursor
- > r: drops down a line and sits
- > ther flashing
- > r: waiting for me to enter
- > another character
- > r: like x before it will throw me
- > back to the
- > r: ide. upon returning to the
- > screen i see
- > r: robert
- > r: x
- > r: robert
- > r:
- > r: i thought scanf() ignored whitespace?
- >
- > First, /n is not white space, it is the character '/' followed by the
- > character 'n'. When /n appears as a part of the format string for
- > scanf, it tells scanf to read and discard the characters / and n if
- > they follow. As the end of line is not a /, the behaviour is not
- > consistent with your code. Check your code before wasting people's
- > time with it.
- >
- > Next, whitespace is not ignored in scanf. It means read and discard
- > all following whitespace. So, scanf has to continue reading till it
- > finds a non-whitespace character, which it leaves unread. This
- > explains the behaviour that you see.
- >
- > Cheers
- > Tanmoy
- > --
- What he's trying to say (but doesn't seem to express very well) is that
- the representation of a "newline" character should have a backward slash
- (the top end pointed left) instead of a forward slash.
-
- That's not the real problem, however. scanf is a really poorly conceived
- function, IMHO. It rules are too complicated and it does not do what most
- people want it to do. For your situation, as shown in the sample code
- above, you want to use gets instead of scanf.
-